package com.reggie.controller;

import com.reggie.pojo.Category;
import com.reggie.pojo.Employee;
import com.reggie.service.CategoryService;
import com.reggie.util.PageUtil;
import com.reggie.util.ResultUtil;
import com.reggie.util.SnowflakeIdWorkerUtil;
import com.reggie.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//菜品分类映射类
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //分页查询多条菜品分类信息
    @GetMapping("/page")
    public ResultUtil<PageUtil<Category>> page(@RequestParam(value = "page", defaultValue = "0") int page,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageUtil<Category> categoryPage = categoryService.getCategoryList(page, pageSize);
        if (categoryPage == null) return ResultUtil.error("未查询到菜品分类！");
        return ResultUtil.success(categoryPage);
    }

    //根据id查询单条菜品分类信息
    @GetMapping("/{id}")
    public ResultUtil<Category> getCategoryById(@PathVariable("id")Long id) {
        Category category = categoryService.getCategoryById(id);
        if (category == null) return ResultUtil.error("未查询到菜品分类！");
        return ResultUtil.success(category);
    }

    //删除菜品分类
    @DeleteMapping
    public ResultUtil<Null> delete(Long id) {
        if (categoryService.delete(id)) return ResultUtil.success(null);
        return ResultUtil.error("删除菜品分类失败！");
    }

    //编辑菜品分类信息
    @PutMapping
    public ResultUtil<Null> edit(@RequestBody Category category){
        Map<String, Object> map = ThreadLocalUtil.get();
        category.setUpdateUser(Long.valueOf((String) map.get("id")));
        if (categoryService.edit(category)) return ResultUtil.success(null);
        return ResultUtil.error("修改菜品分类信息失败！");
    }

    //添加菜品分类信息
    @PostMapping
    public ResultUtil<Null> add(@RequestBody Category category){
        Map<String, Object> map = ThreadLocalUtil.get();
        category.setCreateUser(Long.valueOf((String) map.get("id")));
        category.setUpdateUser(Long.valueOf((String) map.get("id")));
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        category.setId(id);
        if (categoryService.add(category)) return ResultUtil.success(null);
        return ResultUtil.error("添加菜品分类信息失败！");
    }

    //根据类型查询多条菜品分类信息
    @GetMapping("/list")
    public ResultUtil<List<Category>> getCategoryByType(Integer type){
        List<Category> categoryList = categoryService.getCategoryByType(type);
        if (categoryList==null) return ResultUtil.error("未查询到菜品分类！");
        return ResultUtil.success(categoryList);
    }
}
